const ssq = require('../../utils/ssq')
var  arrays=ssq.getList().obj;
Page({
    data: {
        
         switch1Checked:true,
          //三级联动
         citysIndex: [0, 0, 0], //给一个初始值索引，因为有三列，所以3个0
         province:null,
         city:null,
         area:null,
         address:null

    },
    onLoad:function(opt){
       arrays = arrays.slice(1);
       console.log('arrays')
       console.log(arrays)
       var userInfo= wx.getStorageSync('userInfo');
        this.setData({
            userInfo:userInfo,
            code:opt.code
        })
        if (opt.code!=-1) {
          console.log('修改')
          this.getDetail();
        }
      //三级联动
       var that = this;
    
          //定义三列空数组
        var cityArray = [
            [],
            [],
            [],
          ];
          for (let i = 0, len = arrays.length; i < len; i++) {
             cityArray[0].push(arrays[i].name)
          }
          for (let i = 0, len = arrays[0].city.length; i < len; i++) {
             cityArray[1].push( arrays[0].city[i].name)
          }
          for (let i = 0, len = arrays[0].city[0].area.length; i < len; i++) {
             cityArray[2].push( arrays[0].city[0].area[i].name)
          }
         
       
        console.log(cityArray)
        // cityArray=[[1,2,3],[1,2,3],[1,2,3]]
        that.setData({
          cityArray: cityArray
        });
      console.log('初始化完成');
    
   
    },
    getDetail:function(){
       var _self=this;
      
        wx.request({
            url:getApp().globalData.baseUrl+'/addr/details',
            method:"post",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            data:{
              openid:this.data.userInfo.openid,
              id:this.data.code
            },
            success: function(res) {
              if (res.data.code==200) {
                console.log()
                 _self.setData({

                    name:res.data.obj.name,
                    phone:res.data.obj.phone,
                    province:res.data.obj.province,
                    city:res.data.obj.city,
                    address:res.data.obj.address,
                    // del :res.data.obj.del==1?true:false,
                    switch1Checked:res.data.obj.del==1?true:false,
                    yn:1,
                    ssq:res.data.obj.province+res.data.obj.city+res.data.obj.area
                    
                 })
              }
            console.log(res)  
            
         }
       })
    },
    onShow:function(){
       
       // var chooseProvince= wx.getStorageSync('chooseProvince100');
       // var chooseCity2=wx.getStorageSync('chooseCity100');
     
      
       //  if (chooseProvince) {
       //    this.setData({
       //      city:wx.getStorageSync('chooseCity100'),
       //      item:wx.getStorageSync('chooseCity100'),
       //      province:wx.getStorageSync('chooseProvince100')
       //    })
       //  }
    },
    switch1Change:function(e){
    	console.log(e.detail.value)
      this.setData({
      	switch1Checked:e.detail.value
      })
    },
    save:function(){

      if (!this.data.name) {
         wx.showToast({
          title: '请输入姓名',
          icon:'none',
          duration: 2000
        })
        return
      }
      if (this.data.phone.length!=11) {
         wx.showToast({
          title: '请输入手机号',
          icon:'none',
          duration: 2000
        })
        return
      }
      if (!this.data.city) {
         wx.showToast({
          title: '请选择城市',
          icon:'none',
          duration: 2000
        })
        return
      }
      if (!this.data.address) {
         wx.showToast({
          title: '请输入详情地址',
          icon:'none',
          duration: 2000
        })
        return
      }
      var json;
      var url;
      if (this.data.code==-1) {
         json={
            
            openid:this.data.userInfo.openid ,
            name:this.data.name,
            phone:this.data.phone,
            province:this.data.province,
            city:this.data.city,
            address:this.data.address,
            area:this.data.area,
            del :this.data.switch1Checked?1:0,
            yn:1
          }
         url='/addr/insert' 
         
      }else{
            json={
            id:this.data.code,
            openid:this.data.userInfo.openid ,
            name:this.data.name,
            phone:this.data.phone,
            province:this.data.province,
            city:this.data.city,
            area:this.data.area,
            address:this.data.address,
            del :this.data.switch1Checked?1:0,
            yn:1
          }
           url='/addr/update' 
      }
    
    	console.log(json)
    	// 
    	var _self=this;
        wx.request({
            url:getApp().globalData.baseUrl+url,
            method:"post",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            data:json,
            success: function(res) {
              if (res.data.code==200) {
                wx.navigateBack({
                  delta: 1
                })
              }
            console.log(res)  
            
         }
       })
    },
    bindinput1:function(e){
      console.log(e.detail.value)
      this.setData({
      	name:e.detail.value
      })
    },
    bindinput2:function(e){
      this.setData({
      	phone:e.detail.value
      })
    	
    },bindinput3:function(e){
    this.setData({
      	address:e.detail.value
      })
    },
   func_changeCitysChange: function(e) {
    console.log('*************')
   
    var ssq =this.data.cityArray[0][this.data.citysIndex[0]] + this.data.cityArray[1][this.data.citysIndex[1]] + this.data.cityArray[2][this.data.citysIndex[2]];
    this.setData({
        ssq: ssq,//获取选中的省市区
        province:this.data.cityArray[0][this.data.citysIndex[0]],
        city:this.data.cityArray[1][this.data.citysIndex[1]],
        area:this.data.cityArray[2][this.data.citysIndex[2]]
    })
      

    // var address='';
    // if (that.data.ssq == undefined){
    //   //下面方法中没有设置ssq，应该给它默认值 ，此时citysIndex相当于[0,0,0]
    //   var citysIndex = that.data.citysIndex;
    //   for (let i in citysIndex) {
    //     address += cityArray[i][citysIndex[i]]
    //   }
    // }else{
    //   address = that.data.ssq;
    // }
    
  },
  func_changeCitysChangeColumn: function(e) {
    console.log(e)
    console.log('###############')
    var that = this;
    var cityArray = that.data.cityArray;

    var list1 = []; //存放第二列数据，即市的列
    var list2 = []; //存放第三列数据，即区的列
    
    var citysIndex = [];
    //主要是注意地址文件中的字段关系，省、市、区关联的字段有 sheng、di、level
    switch (e.detail.column) {
      case 0:
        var  arr1=[];
        for (let i = 0, len = arrays[e.detail.value].city.length; i < len; i++) {
             arr1.push( arrays[e.detail.value].city[i].name)
        }
        list1=arr1;
        console.log(arr1);
        // console.log(arrays[e.detail.value].city[0])
        var  arr2=[];
        for (let i = 0, len = arrays[e.detail.value].city[0].area.length; i < len; i++) {
            arr2.push(arrays[e.detail.value].city[0].area[i].name)
        }
        list2=arr2;
        console.log(arr2);
        citysIndex = [e.detail.value, 0, 0];
        // var ssq = cityArray[0][that.data.citysIndex[0]] + list1[e.detail.value] + list2[0] + '';
        break;
      case 1:
        //滑动中列
        var  arr2=[];
        for (let i = 0, len = arrays[that.data.citysIndex[0]].city[e.detail.value].area.length; i < len; i++) {
            arr2.push(arrays[that.data.citysIndex[0]].city[e.detail.value].area[i].name)
        }
        list1 = cityArray[1];
        list2=arr2;
        citysIndex = [that.data.citysIndex[0], e.detail.value, 0];
        
        // var ssq = cityArray[0][that.data.citysIndex[0]] + list1[e.detail.value] + list2[0] + '';

        break;
      case 2:
        //滑动右列
        list1 = cityArray[1];
        list2 = cityArray[2];
        citysIndex = [that.data.citysIndex[0], that.data.citysIndex[1], e.detail.value];

        // var ssq = cityArray[0][that.data.citysIndex[0]] + list1[that.data.citysIndex[1]] + list2[e.detail.value] + '';
        break;
    }
    console.log('ssq')
    console.log(citysIndex)
    console.log(ssq)
    var ssq =cityArray[0][citysIndex[0]] + list1[citysIndex[1]] + list2[citysIndex[2]];
    that.setData({
      "cityArray[1]": list1,//重新赋值中列数组，即联动了市
      "cityArray[2]": list2,//重新赋值右列数组，即联动了区
      citysIndex: citysIndex,//更新索引
     
    });
    console.log('this.data')
    console.log(this.data)
  },
});